#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ShootThemUp.Sources.ScreenManagement;
using ShootThemUp.Sources.Screens;
using ShootThemUp.Sources.AudioElements;
#endregion

namespace ShootThemUp.Sources
{
    /// <summary>
    /// Sample showing how to manage different game states, with transitions
    /// between menu screens, a loading screen, the game itself, and a pause
    /// menu. This main game class is extremely simple: all the interesting
    /// stuff happens in the ScreenManager component.
    /// </summary>
    public class GameStateManagement : Microsoft.Xna.Framework.Game
    {
        #region Constant Fields

        /// <summary>
        /// The width of the screen in portrait mode, in pixel.
        /// </summary>
        public const int DEFAULT_PORTRAIT_SCREEN_WIDTH = 480;
        /// <summary>
        /// The height of the screen in portrait mode, in pixel.
        /// </summary>
        public const int DEFAULT_PORTRAIT_SCREEN_HEIGHT = 800;
        /// <summary>
        /// The border of the screen, in pixel.
        /// </summary>
        public const int BORDER = 10;
        /// <summary>
        /// The name of this application.
        /// </summary>
        public const string APP_NAME = "Pineapple Armageddon";

        #endregion

        #region Fields

        private GraphicsDeviceManager graphics;
        private ScreenManager screenManager;

        #endregion

        #region Initialization

        /// <summary>
        /// The main game constructor.
        /// </summary>
        public GameStateManagement()
        {
            Content.RootDirectory = "Content";

            graphics = new GraphicsDeviceManager(this);
            graphics.IsFullScreen = true;
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Landspace mode for graphics
            InitializePortraitGraphics();

            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            // Audio Loading
            Audio audio = Audio.GetInstance;
            audio.LoadSound(Content);

            // attempt to deserialize the screen manager from disk. if that
            // fails, we add our default screens.
            if (!screenManager.DeserializeState())
            {
                // Activate the first screens.
                screenManager.AddScreen(new BackgroundScreen(@"Menus\Textures2D\title"), null);
                    screenManager.AddScreen(new TitleScreen(), null);
            }
        }

        /// <summary>
        /// Exit handler. Serialize the screenManager.
        /// </summary>
        /// <param name="sender">The exit event sender.</param>
        /// <param name="args">The event arguments.</param>
        protected override void OnExiting(object sender, System.EventArgs args)
        {
            // serialize the screen manager whenever the game exits
            screenManager.SerializeState();

            base.OnExiting(sender, args);
        }

        /// <summary>
        /// Helper method to the initialize the game to be a portrait game.
        /// </summary>
        private void InitializePortraitGraphics()
        {
            graphics.PreferredBackBufferWidth = GameStateManagement.DEFAULT_PORTRAIT_SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = GameStateManagement.DEFAULT_PORTRAIT_SCREEN_HEIGHT;
        }

        /// <summary>
        /// Helper method to initialize the game to be a landscape game.
        /// </summary>
        private void InitializeLandscapeGraphics()
        {
            graphics.PreferredBackBufferWidth = GameStateManagement.DEFAULT_PORTRAIT_SCREEN_HEIGHT;
            graphics.PreferredBackBufferHeight = GameStateManagement.DEFAULT_PORTRAIT_SCREEN_WIDTH;
        }

        #endregion

        #region Draw

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            // The real drawing happens inside the screen manager component.
            base.Draw(gameTime);
        }

        #endregion
    }

    #region Entry Point

    /// <summary>
    /// The main entry point for the application. Run the game.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            using (GameStateManagement game = new GameStateManagement())
            {
                game.Run();
            }
        }
    }

    #endregion
}
